// pages/map/map.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    longitude: 120.382833,
    latitude: 30.31652,
    markers: [],
    currentLocation: {},
    mapContext: null
  },

  onReady() {
    let obj = { oooo: 100 }
    this.setData({
      mapContext: wx.createMapContext('map')
    })
  },

  async getCenterLocation() {
    const res = await this.data.mapContext.getCenterLocation()

    this.setData({
      currentLocation: res
    })
  },

  moveToLocation() {
    this.data.mapContext.moveToLocation({
      success: (res) => {
        console.log(res)
      }
    })
  },

  async addMarkers() {
    await this.getCenterLocation()

    let arr = [
      ...this.data.markers,
      {
        id: Date.now(),
        longitude: this.data.currentLocation.longitude,
        latitude: this.data.currentLocation.latitude,
        iconPath: '../../images/定位.png',
        width: 40,
        height: 40
      }
    ]

    this.setData({
      markers: arr
    })

    // this.data.mapContext.addMarkers({
    //   markers: arr,
    //   success: (res) => {
    //     console.log(res)
    //   },
    //   fail: (err) => {
    //     console.log(err)
    //   }
    // })
  }
})
